﻿namespace AnyOffice.Components
{
    using Microsoft.ApplicationBlocks.Data;
    using System;
    using System.Data;

    public class SecurityManager
    {
        public static bool IsCanManage(int deptId)
        {
            ConfigManager manager = new ConfigManager();
            UserCache cache = new UserCache();
            if (cache.PrivId != manager.AdministratorPrivId)
            {
                string postPriv = cache.PostPriv;
                if (postPriv == null) return false;
                if (postPriv != "0") return (postPriv == "1" || postPriv == "2" && ("," + cache.PostDept + ",").IndexOf("," + deptId.ToString() + ",") >= 0);
                if (cache.DeptId != deptId) return false;
            }
            return true;
        }

        public static bool IsCanManage(string userId)
        {
            ConfigManager manager = new ConfigManager();
            UserCache cache = new UserCache();
            if (cache.PrivId != manager.AdministratorPrivId)
            {
                int num = -1;
                string str = "SELECT [DeptId] FROM [dbo].[Users] WHERE [UserId] = '" + userId + "'";
                object obj2 = SqlHelper.ExecuteScalar(ConfigManager.ConnectionString, CommandType.Text, str);
                if (obj2 != null) num = Convert.ToInt32(obj2);
                string postPriv = cache.PostPriv;
                if (postPriv == null) return false;
                if (postPriv != "0") return (postPriv == "1" || postPriv == "2" && ("," + cache.PostDept + ",").IndexOf("," + num.ToString() + ",") >= 0);
                if (cache.DeptId != num) return false;
            }
            return true;
        }
    }
}

